package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BabyInviteActivity extends BaseActivity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baby_invite);
		
		((TextView) findViewById(R.id.center)).setTextColor(Color.WHITE);
		((TextView) findViewById(R.id.center)).setText(getString(R.string.invite0));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch(arg0.getId())
		{
		case R.id.left:
			finish();
			break;
			
		case R.id.right:
			sendHttpRequest();
			break;
		}
	}

	private void sendHttpRequest() {
		final String email = ((EditText)findViewById(R.id.text1)).getText().toString();
		if(!Utils.isEmail(email) && !Utils.isMobileNO(email))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_email);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("resCode").equals("1000")) {
					if(ruser.getString("message").equals("unexist"))
					{
						ToastUtil.showToastWithAlertPic(R.string.account_unexict);
					}
					else
					{
						ToastUtil.showToastWithOkPic(R.string.apply_success);
						finish();
					}
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("applyUser", email);
		params.put("handType", "invitation");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/ApplyToOtherUser.ashx", JSON.toJSONString(params), loginHandler);
	}
}
